/**
 * 2018年11月21日下午4:34:00
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.JsonKit;
import com.baihang.project.ds.model.LouDongModel;
import com.baihang.project.ds.service.LouDongService;
/**
 * LouDong 楼栋
 */
@Controller
@RequestMapping("/loudong")
public class LouDongController extends BaseController {
	private static Logger logger =  LoggerFactory.getLogger(LouDongController.class);
	@Autowired
	private LouDongService service; 
	
	@RequestMapping
	public String index(Model model) {		
		model.addAttribute("renchous", service.getRenChou());
		
		return "/project/ds/loudong/index";
	}
	
	
	@RequestMapping(value ="/getTree", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getTree() {
		String tree = service.getTree();
		//System.out.println(tree);
		return tree;
	}
	
	@RequestMapping("/checkExistLouDong")
	@ResponseBody
	public String checkExistLouDong(@RequestParam("T_BanName") String name) {
		String result ="true";
		if(!service.checkExistLouDong(name)) {
			result="false";
		}
		return result;
	}
	
	@RequestMapping(value="/addLouDong",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String addLouDong(LouDongModel model) {
		String result ="true";
		if(!service.addLouDong(model)) {
			result="false";
		}		
		return String.format("{\"result\":\"%s\"}", result);
	}
	
	@RequestMapping(value="/saveLouDong",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String saveLouDong(LouDongModel model) {
		String result ="true";
		if(!service.saveLouDong(model)) {
			result="false";
		}		
		return String.format("{\"result\":\"%s\"}", result);
	}
	
	@RequestMapping(value="/deleteLouDong",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteLouDong(int id) {
		String result ="true";
		try {
			if(!service.deleteLouDong(id)) {
				result="false";
			}		
		}catch (Exception e) {
			logger.info("删除楼栋{}失败",id);
			result="false";
		}
		return String.format("{\"result\":\"%s\"}", result);
	}
	
	@RequestMapping(value="/getLouDongMaxOrder",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getLouDongMaxOrder() {
		
		int maxOrder = service.getMaxOrder();
		return JsonKit.toDoubleJson2("true", String.valueOf(maxOrder));
	}

	
	
}
